package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Company1;
import com.woniu.entity.Graduate1;
import com.woniu.entity.Emp1;
import com.woniu.entity.Student1;
import com.woniu.pojo.Company;
import com.woniu.pojo.Student;
import com.woniu.service.CompanyService;
import com.woniu.service.EmpService;
import com.woniu.service.StudentService;
import com.woniu.utils.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private StudentService studentService;
    @Autowired
    EmpService empService;

    //展示页面跳转
    @RequestMapping("listJump")
    public String listJump(){
        return "WEB-INF/company/list";
    }

    //增加页面跳转
    @RequestMapping("addJump")
    public String addJump(){
        return "WEB-INF/company/add";
    }

    //增加公司
    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object> add(Company company){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            companyService.add(company);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    //按主键删除公司
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String,Object> deleteById(Integer companyId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            companyService.deleteById(companyId);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    //查询一个公司
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String,Object> findOne(Integer companyId,Model model){
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            Company company = companyService.findOne(companyId);
            map.put("company",company);
            map.put("result","ok");
        }catch(Exception e){
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    //跳转到修改
    @RequestMapping("updateJump")
    public String updateJump(Model model, Integer companyId){
        Company company = companyService.findOne(companyId);
        model.addAttribute("company",company);
        return "WEB-INF/company/update";
    }

    //修改公司信息
    @RequestMapping("update")
    @ResponseBody
    public Map<String, Object> update(Company company){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            companyService.updateById(company);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    //公司查询分页
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize,Company company){
        System.out.println("===================================================");
        System.out.println("company = " + company);
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Company1> companyList = companyService.findAll(company);
            PageInfo<Company1> pageInfo=new PageInfo<Company1>(companyList);
            map.put("pageInfo",pageInfo);
            map.put("companyList",companyList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    //公司批量删除
    @RequestMapping("deleteByExample")
    @ResponseBody
    public Map<String,Object> deleteByExample(Integer[] companyIds){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            companyService.deleteByExample(Arrays.asList(companyIds));
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("exportCompany")
    public void exportCompany(HttpServletRequest request, HttpServletResponse response){
        List<Company1> companyList = companyService.findAll(null);
        String[] title = {"ID","用户ID","公司名称","公司详情","公司电话"};
        String fileName = "company.xls";
        String sheetName = "sheet1";
        String[][] content = new String[companyList.size()][5];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            for (int i = 0;i < companyList.size();i++){
                content[i][0] = String.valueOf(companyList.get(i).getCompanyId());
                content[i][1] = companyList.get(i).getUscerId() == null ? "" : String.valueOf(companyList.get(i).getUscerId().getUserId());
                content[i][2] = companyList.get(0).getCompanyName();
                content[i][3] = companyList.get(0).getCompanyDetail();
                content[i][4] = companyList.get(0).getCompanyPhone();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName,title,content,null);
        try {
            this.setResponseHeader(response,fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            fileName = new String(fileName.getBytes(),"utf-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
    }

    //跳转到修改  公司个信息修改

    @RequestMapping("updateJump1")
    public String updateJump1(Model model, Integer companyId){
        Map<String,Object> map = new HashMap<String,Object>();
        Company company = companyService.findOne(companyId);
        Emp1 one = empService.findOne(company.getEmpId());
        map.put("company",company);
        map.put("emp",one);
        model.addAttribute("map",map);
        return "WEB-INF/info/companyUpdate";
    }
    // 修改公司信息
    @RequestMapping("findOne1")
    @ResponseBody
    public Map<String,Object> findOne1(Integer companyId,Model model){
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            Company company = companyService.findOne(companyId);
            Emp1 one = empService.findOne(company.getEmpId());
            map.put("company",company);
            map.put("emp",one);
            map.put("result","ok");
        }catch(Exception e){
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }
    //个人修改公司信息
    @RequestMapping("update1")
    @ResponseBody
    public Map<String, Object> update1(Company company){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            companyService.updateById1(company);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
}
